package com.ysm.smfu.adapter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ysm.smfu.R;
import com.ysm.smfu.bean.Friend;
import com.ysm.smfu.manager.GlideCircleTransform;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.SwipeLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import chatuidemo.ui.ChatActivity;
import easeui.EaseConstant;

/**
 * Created by Administrator on 2016/10/25.
 */
public class ContactAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {
    private Activity activity;
    private List<Friend> friends;
    private ListView listView;
    private SwipeLayout preLayout;
    private OnDelMsg onDelMsg;

    public ContactAdapter(List<Friend> datas, ListView listView, Activity activity) {
        friends = datas;
        this.listView = listView;
        listView.setOnItemClickListener(this);
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return friends.size();
    }

    @Override
    public Object getItem(int position) {
        return friends.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = UIUtils.inflate(R.layout.list_item_contactlist);
            viewHolder = new ViewHolder(convertView);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final Friend friend = friends.get(position);
        //        上一个对象
        Friend preFriend = null;
        if (position == 0) {
            preFriend = null;//索引为0的 对象 上一个对象 就没有
        } else {
            preFriend = friends.get(position - 1);
        }
        boolean isShowPinYin = true;
        if (preFriend == null) {
            isShowPinYin = true;
        } else {
            if (friend.getPinYin().charAt(0) == preFriend.getPinYin().charAt(0)) {
                isShowPinYin = false;
            }
        }
        viewHolder.tv_pinyin.setVisibility(isShowPinYin ? View.VISIBLE : View.GONE);
        viewHolder.name.setText(friend.petName);
        viewHolder.tv_pinyin.setText(String.valueOf(friend.getPinYin().charAt(0)));
        if (!TextUtils.isEmpty(friend.imageAddress) && !friend.imageAddress.startsWith("null?")) {
            Glide.with(activity).load(friend.imageAddress).placeholder(R.drawable.myimg2)
                    .transform(new GlideCircleTransform(UIUtils.getContext()))
                    .error(R.drawable.myimg2).into(viewHolder.headImg);
        } else
            viewHolder.headImg.setImageResource(R.drawable.myimg2);
        viewHolder.headImg.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onDelMsg != null) {
                            onDelMsg.onImageClick(position);
                        }
                    }
                }
        );
        viewHolder.swipeLayout.setOnSwipeChangeListener(new SwipeLayout.OnSwipeChangeListener() {


            @Override
            public void onOpen(SwipeLayout layout) {
                preLayout = layout;
            }

            @Override
            public void onClose(SwipeLayout layout) {
            }

            @Override
            public void onSwipng(SwipeLayout layout) {
            }

            @Override
            public void onStartOpen(SwipeLayout layout) {
                if (preLayout != null) {
                    preLayout.close();
                }
            }

            @Override
            public void onStartClose(SwipeLayout layout) {

            }
        });
//        删除联系人
        viewHolder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onDelMsg != null) {
                    onDelMsg.onDelMsg(position);
                }
            }
        });
        /**
         * 点击条目事件
         */
        viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onDelMsg != null) {
                    onDelMsg.onOclikItem(position);
                    if (preLayout != null) {
                        preLayout.close();
                    }
                }
            }
        });
        return convertView;
    }

    //    删除消息回调
    public void setOnDelMsg(OnDelMsg onDelMsg) {
        this.onDelMsg = onDelMsg;
    }

    public interface OnDelMsg {
        public void onDelMsg(int position);

        public void onOclikItem(int position);

        public void onImageClick(int position);
    }

    class ViewHolder {
        private ImageView headImg;
        private TextView name;
        private TextView tv_pinyin;
        private SwipeLayout swipeLayout;
        private AutoRelativeLayout item;
        private TextView del;

        public ViewHolder(View view) {
            headImg = (ImageView) view.findViewById(R.id.headImg);
            name = (TextView) view.findViewById(R.id.name);
            tv_pinyin = (TextView) view.findViewById(R.id.tv_pinyin);
            swipeLayout = (SwipeLayout) view.findViewById(R.id.swipeLayout);
            item = (AutoRelativeLayout) view.findViewById(R.id.item);
            del = (TextView) view.findViewById(R.id.del);
            view.setTag(this);
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position > listView.getHeaderViewsCount()) {
            position -= listView.getHeaderViewsCount();
            Friend friend = friends.get(position);
//            跳转到聊天页面,传递好友id 还是跳转到详情界面？
            UIUtils.startActivity(new Intent(UIUtils.getContext(), ChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, friend.hxId));
        }

    }

    public void setData(List<Friend> datas) {
        friends = datas;
    }
}
